package com.lsyw.intergrate.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lsyw.intergrate.common.util.DateUtil;
import com.lsyw.intergrate.entity.MSamplingTask;
import com.lsyw.intergrate.entity.Massif;
import com.lsyw.intergrate.entity.MassifPlan;
import com.lsyw.intergrate.entity.MassifPlanAlarm;
import com.lsyw.intergrate.entity.qo.MassifPlanQo;
import com.lsyw.intergrate.service.MassifPlanAlarmService;
import com.lsyw.intergrate.service.MassifService;
import org.springframework.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.lsyw.intergrate.entity.common.PageInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lsyw.intergrate.common.Result;
import org.springframework.util.CollectionUtils;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import com.lsyw.intergrate.service.MassifPlanService;

import java.util.*;

import io.swagger.annotations.Api;
import lombok.Data;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 工地修复进度计划 前端控制器
 * </p>
 *
 * @author 刘磊
 * @since 2024-02-21
 */
@Api(tags = "工地修复进度计划")
@RestController
@RequestMapping("/massif-plan")
public class MassifPlanController {


    @Autowired
    private MassifPlanService massifPlanService;
    @Autowired
    private MassifPlanAlarmService massifPlanAlarmService;
    @Autowired
    private MassifService massifService;

    /**
     * 通过id查询
     */
    @GetMapping("/test")
    public Result test() {
        List<MassifPlan> list1=massifService.getAllAlarm();
        list1.forEach(e->{
            Massif massif = massifService.getById(e.getMassifId());
            MassifPlanAlarm massifPlanAlarm=new MassifPlanAlarm();
            massifPlanAlarm.setDate(new Date());
            massifPlanAlarm.setStatus(2);
            massifPlanAlarm.setDelFlag(0);
            massifPlanAlarm.setGmtCreate(new Date());
            massifPlanAlarm.setMassifPlanId(e.getId());
            String content =massif.getName()+"计划"+ DateUtil.format(e.getDate()) +"完成的土壤修复"+e.getSoilPercent()+"% "+massif.getTrxfzfl() * e.getSoilPercent() / 100+"m³，地表水修复"+e.getWaterPercent()+"% "+massif.getDxsxfl() * e.getWaterPercent() / 100+"m³未修复完成，请注意该计划落实情况";

            massifPlanAlarm.setContent(content);
            massifPlanAlarmService.save(massifPlanAlarm);
        });
        List<MassifPlan> list2=massifService.getWaterAlarm();
        list2.forEach(e->{
            Massif massif = massifService.getById(e.getMassifId());
            MassifPlanAlarm massifPlanAlarm=new MassifPlanAlarm();
            massifPlanAlarm.setDate(new Date());
            massifPlanAlarm.setStatus(2);
            massifPlanAlarm.setDelFlag(0);
            massifPlanAlarm.setGmtCreate(new Date());
            massifPlanAlarm.setMassifPlanId(e.getId());
            String content =massif.getName()+"计划"+ DateUtil.format(e.getDate()) +"完成的地表水修复"+e.getWaterPercent()+"% "+massif.getDxsxfl() * e.getWaterPercent() / 100+"m³未修复完成，请注意该计划落实情况";

            massifPlanAlarm.setContent(content);
            massifPlanAlarmService.save(massifPlanAlarm);
        });
        List<MassifPlan> list3=massifService.getSoilAlarm();
        list3.forEach(e->{
            Massif massif = massifService.getById(e.getMassifId());
            MassifPlanAlarm massifPlanAlarm=new MassifPlanAlarm();
            massifPlanAlarm.setDate(new Date());
            massifPlanAlarm.setStatus(2);
            massifPlanAlarm.setDelFlag(0);
            massifPlanAlarm.setGmtCreate(new Date());
            massifPlanAlarm.setMassifPlanId(e.getId());
            String content =massif.getName()+"计划"+ DateUtil.format(e.getDate()) +"完成的土壤修复"+e.getSoilPercent()+"% "+massif.getTrxfzfl() * e.getSoilPercent() / 100+"m³未修复完成，请注意该计划落实情况";

            massifPlanAlarm.setContent(content);
            massifPlanAlarmService.save(massifPlanAlarm);
        });
        return Result.successResult(null);
    }
    /**
     * 通过id查询
     */
    @GetMapping("/get-by-id")
    public Result getById(String id) {
        return Result.successResult(massifPlanService.getById(id));
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    public Result save(@RequestBody MassifPlanQo qo) {
        if (!CollectionUtils.isEmpty(qo.getPlanList())) {
            qo.getPlanList().forEach(e -> {
                e.setMassifId(qo.getMassifId());
                e.setDelFlag(0);
                e.setGmtCreate(new Date());
                massifPlanService.save(e);
            });

        }

        return Result.successResult(null);
    }

    /**
     * 通过id删除
     */
    @GetMapping("/delete-by-id/{id}")
    public Result delete(@PathVariable(value = "id") String id) {
        MassifPlan massifPlan = this.massifPlanService.getById(Long.parseLong(id));
        massifPlan.setDelFlag(1);
        this.massifPlanService.updateById(massifPlan);
        return Result.successResult(null);
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Result updateById(@RequestBody MassifPlan massifPlan) {
        massifPlan.setGmtModified(new Date());
        massifPlanService.updateById(massifPlan);
        return Result.successResult(null);
    }


    /**
     * 查询列表
     */
    @PostMapping("/list")
    public Result list(@RequestBody MassifPlanReqVo massifPlan) {
        final LambdaQueryWrapper<MassifPlan> lambda = new QueryWrapper<MassifPlan>().lambda();
        this.buildCondition(lambda, massifPlan);
        Massif massif = massifService.getById(massifPlan.getMassifId());
        List<MassifPlan> list = massifPlanService.list(lambda);
        list.forEach(e -> {
            if (Objects.nonNull(massif.getTrxfzfl())) {
                e.setSoilValue(massif.getTrxfzfl() * e.getSoilPercent() / 100);
            }
            if (Objects.nonNull(massif.getDxsxfl())) {
                e.setWaterValue(massif.getDxsxfl() * e.getWaterPercent() / 100);
            }

        });
        return Result.successResult(list);
    }

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public Result page(@RequestBody PageInfo pageInfo) {
        Page<MassifPlan> page = new Page<>();
        page.setSize(pageInfo.getPageSize());
        page.setCurrent(pageInfo.getPageNum());
        final LambdaQueryWrapper<MassifPlan> lambda = new QueryWrapper<MassifPlan>().lambda();
        //TODO 自己的查询逻辑
        Map<String, Object> condition = pageInfo.getCondition();
        if (condition.get("massifId") != null && !StringUtils.isEmpty(condition.get("massifId").toString())) {
            lambda.like(MassifPlan::getMassifId, condition.get("massifId"));
        }
        lambda.eq(MassifPlan::getDelFlag, 0);
        Massif massif = massifService.getById(condition.get("massifId").toString());
        IPage<MassifPlan> iPage = this.massifPlanService.page(page, lambda);
        iPage.getRecords().forEach(e -> {
            if (Objects.nonNull(massif.getTrxfzfl())) {
                e.setSoilValue(massif.getTrxfzfl() * e.getSoilPercent() / 100);
            }
            if (Objects.nonNull(massif.getDxsxfl())) {
                e.setWaterValue(massif.getDxsxfl() * e.getWaterPercent() / 100);
            }

        });
        return Result.successResult(iPage);
    }


    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<MassifPlan> lambda, MassifPlanReqVo param) {
        if (!StringUtils.isEmpty(param.getId())) {
            lambda.eq(MassifPlan::getId, param.getId());
        }
        if (!CollectionUtils.isEmpty(param.getDateList())) {
            lambda.ge(MassifPlan::getDate, param.getDateList().get(0));
            lambda.le(MassifPlan::getDate, param.getDateList().get(1));
        }
        if (!StringUtils.isEmpty(param.getSoilPercent())) {
            lambda.eq(MassifPlan::getSoilPercent, param.getSoilPercent());
        }
        if (!StringUtils.isEmpty(param.getWaterPercent())) {
            lambda.eq(MassifPlan::getWaterPercent, param.getWaterPercent());
        }
        if (!StringUtils.isEmpty(param.getMassifId())) {
            lambda.eq(MassifPlan::getMassifId, param.getMassifId());
        }
        if (!StringUtils.isEmpty(param.getSort())) {
            lambda.eq(MassifPlan::getSort, param.getSort());
        }
        if (!StringUtils.isEmpty(param.getStatus())) {
            lambda.eq(MassifPlan::getStatus, param.getStatus());
        }
        if (!StringUtils.isEmpty(param.getGmtCreateUser())) {
            lambda.eq(MassifPlan::getGmtCreateUser, param.getGmtCreateUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtCreateList())) {
            lambda.ge(MassifPlan::getGmtCreate, param.getGmtCreateList().get(0));
            lambda.le(MassifPlan::getGmtCreate, param.getGmtCreateList().get(1));
        }
        if (!StringUtils.isEmpty(param.getGmtModifiedUser())) {
            lambda.eq(MassifPlan::getGmtModifiedUser, param.getGmtModifiedUser());
        }
        if (!CollectionUtils.isEmpty(param.getGmtModifiedList())) {
            lambda.ge(MassifPlan::getGmtModified, param.getGmtModifiedList().get(0));
            lambda.le(MassifPlan::getGmtModified, param.getGmtModifiedList().get(1));
        }
        lambda.eq(MassifPlan::getDelFlag, 0);
        lambda.orderBy(true, false, MassifPlan::getId);
    }


    /**
     * 请求model
     */
    @Data
    @AllArgsConstructor
    private static class MassifPlanReqVo extends MassifPlan {
        private List<String> dateList; // 日期起止
        private List<String> gmtCreateList; // 创建时间起止
        private List<String> gmtModifiedList; // 更新时间起止
    }


}
